package com.aiju.cash.ui.stockupdate.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.multiple_shops.widget.MenuLinearLayout;
import com.aiju.cash.ui.stockupdate.History;

import java.util.List;

/**
 * 作者：流冥 on 2017/6/30 13:58
 * 说明：
 */

public class HistoryAdapter extends BaseAdapter {
    private List<History> mList;
    private Context mContext;

    public HistoryAdapter(Context context, List<History> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        History history = (History) getItem(position);
        final String item = history.getName();

        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_historty, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, MenuLinearLayout.dip2px(mContext, 44));
            convertView.setLayoutParams(layoutParams);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.pic = (ImageView) convertView.findViewById(R.id.del);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.name.setText(item);

        viewHolder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(item);
            }
        });
        viewHolder.pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.delHistory(item);
            }
        });


        return convertView;
    }

    class ViewHolder {
        ImageView pic;
        TextView name;
    }

    public interface HistoryListener {
        void delHistory(String value);

        void onItemClick(String value);
    }

    HistoryListener mListener;

    public void setmListener(HistoryListener listener) {
        mListener = listener;
    }

    public void setList(List<History> list) {
        mList = list;
        notifyDataSetChanged();
    }

}
